//
//  IWPopMenuView.m
//  WeiBo17
//
//  Created by teacher on 15/8/17.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#import "IWPopMenuView.h"

@interface IWPopMenuView()

@property (nonatomic, weak) UIImageView *bgView;

@end

@implementation IWPopMenuView

- (instancetype)initWithCustomView:(UIView *)customView
{
    self = [super init];
    if (self) {
        self.size = [UIScreen mainScreen].bounds.size;
        [self addTarget:self action:@selector(hide:) forControlEvents:UIControlEventTouchUpInside];
//        UIButton *coverButton = [[UIButton alloc] init];
//        coverButton.size = self.size;
//        //设置蒙板效果
//        coverButton.backgroundColor = [UIColor clearColor];
        //添加点击事件
        
//        [coverButton addTarget:self action:@selector(hide:) forControlEvents:UIControlEventTouchUpInside];
//        //添加蒙板
//        [self addSubview:coverButton];
        
//        self.backgroundColor = [UIColor clearColor];
        
        //初始化小灰框
        UIImageView *imageView = [[UIImageView alloc] init];
        UIImage *image = [UIImage imageNamed:@"popover_background"];
        //设置可以接收用户的点击事件
        imageView.userInteractionEnabled = YES;
        imageView.image = image;
        imageView.size = CGSizeMake(customView.width + 10, customView.height + 20);
        
        customView.x = 5;
        customView.y = 12;
        
        [imageView addSubview:customView];
        
        [self addSubview:imageView];
        self.bgView = imageView;
        
    }
    return self;
}


- (void)showWithView:(UIView *)targetView{
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    //把button的坐标转换到屏幕坐标
    //    __ convertRect:__ toView:__
    CGRect rect = [targetView convertRect:targetView.bounds toView:window];
    
    self.bgView.centerX = CGRectGetMidX(rect);
    self.bgView.y = CGRectGetMaxY(rect);
    
    [window addSubview:self];
    
}
- (void)hide:(UIButton *)btn{
    //隐藏其实就是把当前的这个View从父控件里面移除就可以了
    [self removeFromSuperview];
}

@end
